class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        # 调换顺序让小的数组被遍历，两个目的：
        # 1、为了能更简单地取到O(log(min(m,n)))更优的结果
        # 2、如果遍历的是较长的数组下标，预期需要额外判断下标不要超过m + n的一半，否则数组越界
        if len(nums1) > len(nums2):
            return self.findMedianSortedArrays(nums2, nums1)

        m, n = len(nums1), len(nums2)
        left, right = 0, m
        median1, median2 = 0, 0
        while left <= right:
            i = (left + right) // 2
            j = (m + n + 1) // 2 - i
            nums_im1 = (-inf if i == 0 else nums1[i - 1])
            nums_i = (inf if i == m else nums1[i])
            nums_jm1 = (-inf if j == 0 else nums2[j - 1])
            nums_j = (inf if j == n else nums2[j])

            if nums_im1 <= nums_j:
                median1, median2 = max(nums_im1, nums_jm1), min(nums_i, nums_j)
                left = i + 1
            else:
                right = i - 1

        return (median1 + median2) / 2 if (m + n) % 2 == 0 else median1